package com.example.admin.goodnews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.SouSuoJieGuoDataEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemClickListener;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;

/**
 * 歌曲搜索结果适配器
 * Created by admin on 2016/10/18.
 */

public class SouSuoJieGuoAdapter extends RecyclerView.Adapter<SouSuoJieGuoAdapter.ViewHolder> {
    private Context context;
    private SouSuoJieGuoDataEntity souSuoJieGuoDataEntity;

    public SouSuoJieGuoAdapter(Context context, SouSuoJieGuoDataEntity souSuoJieGuoDataEntity) {
        this.context = context;
        this.souSuoJieGuoDataEntity = souSuoJieGuoDataEntity;
    }

    private RecyclerViewItemClickListener recyclerViewItemClickListener;
    private RecyclerViewItemLongClickListener recyclerViewItemLongClickListener;

    public void setRecyclerViewItemClickListener(RecyclerViewItemClickListener recyclerViewItemClickListener) {
        this.recyclerViewItemClickListener = recyclerViewItemClickListener;
    }

    public void setRecyclerViewItemLongClickListener(RecyclerViewItemLongClickListener recyclerViewItemLongClickListener) {
        this.recyclerViewItemLongClickListener = recyclerViewItemLongClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_sousuojieguo, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.tv_sousuojieguo_title.setText("歌曲名：" + souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().get(position).getTitle());
        holder.tv_sousuojieguo_singer.setText("歌手名：" + souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().get(position).getSinger_name());
        holder.tv_sousuojieguo_album.setText("专辑名：" + souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().get(position).getAlbum_name());
        if (recyclerViewItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition();
                    recyclerViewItemClickListener.RecyclerViewItemClickListener(clickposition);
                }
            });
        }
        if (recyclerViewItemLongClickListener != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        int clickposition = holder.getLayoutPosition();
                        recyclerViewItemLongClickListener.RecyclerViewItemLongClickListener(clickposition);
                    }
                    return true;
                }
            });
        }
    }

    // 获取item的title
    public String getTitle(int position) {
        String title = souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().get(position).getTitle();
        return title;
    }

    // 获取item的link
    public String getLink(int position) {
        String link = souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().get(position).getLink();
        return link;
    }

    @Override
    public int getItemCount() {
        return souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_sousuojieguo_title;// 歌曲名
        private TextView tv_sousuojieguo_singer;// 歌手名
        private TextView tv_sousuojieguo_album;// 专辑名

        public ViewHolder(View itemView) {
            super(itemView);
            tv_sousuojieguo_title = (TextView) itemView.findViewById(R.id.tv_sousuojieguo_title);// 歌曲名
            tv_sousuojieguo_singer = (TextView) itemView.findViewById(R.id.tv_sousuojieguo_singer);// 歌手名
            tv_sousuojieguo_album = (TextView) itemView.findViewById(R.id.tv_sousuojieguo_album);// 专辑名
        }
    }
}
